//
import * as GoldenLayout from "golden-layout";
//
import * as mvc from "common/mvc";
import * as dec from "common/decorator";
//
import * as dbtps from "../types";
import * as language from "../language";
import Config from "./config";
import User from "./user";
import Document from "./document";
/**
 * 应用数据。
 */
export default class Application extends mvc.Model {
    public static toString(): string {
        return "[class dragonBones.model.Application]";
    }
    /**
     * 应用版本号。
     */
    public readonly version: string = "6.0.0";
    /**
     * 所有的文档。
     */
    public readonly documents: Document[] = [];
    /**
     * 设置。
     */
    public readonly setting: Setting = Setting.getInstance(Setting);
    /**
     * 用户数据。
     */
    public readonly user: User = User.getInstance(User);
    /**
     * 布局。（需要外部同步数据）
     */
    @dec.getterSetter(true, true)
    public layout: any = whileConfig;
    /**
     * 当前激活的文档。（只有被激活的文档才可以被操作数据）
     */
    @dec.addHistoryAndNotification(false, dbtps.MNotificationType.ApplicationActiveDocumentChange)
    @dec.getterSetter(true, true)
    public currentDocument: Document | null;
    /**
     * 新建的文档总数。（为新文档后缀命名）
     */
    private _createDocumentCount: number = 1;

    private _addDocument(value: Document): void {
        console.assert(this.documents.indexOf(value) < 0);
        this.documents.push(value);
        this._sendNotification(dbtps.MNotificationType.ApplicationOpenDocument, value);
    }

    protected _onClear(): void {
    }
    /**
     * 创建一个新的文档。
     */
    public createDocument(): Document {
        const value = Document.create(Document);
        dec.history.locked = true;
        value.title = language.get(language.Layout.title.document, this._createDocumentCount++);
        dec.history.locked = false;
        this._addDocument(value);

        return value;
    }
    /**
     * 打开文档。
     */
    public openDocument(url: string): Document | null {
        // 需要判断是否已经打开
        return null;
    }
    /**
     * 关闭文档。
     */
    public closeDocument(value: Document): void {
        const index = this.documents.indexOf(value);
        console.assert(index >= 0);

        this.documents.splice(index, 1);
        this._sendNotification(dbtps.MNotificationType.ApplicationCloseDocument, value);
        value.dispose();
    }
    /**
     * 关闭所有文档。
     */
    public closeAllDocument(): void {
        let i = this.documents.length; // 倒序关闭文档。
        while (i--) {
            this.closeDocument(this.documents[i]);
        }
    }
    /**
     * 通过 id 获取指定文档。
     */
    public getDocument(id: string): Document | null {
        for (const document of this.documents) {
            if (document.id === id) {
                return document;
            }
        }

        return null;
    }

    public get dbtps() {
        return dbtps;
    }
};

class Setting extends mvc.Model {
    public static toString(): string {
        return "[class dragonBones.model.Setting]";
    }

    protected _onClear(): void {
    }
}

const whileConfig: GoldenLayout.Config = {
    settings: {
        hasHeaders: true,
        constrainDragToContainer: true,
        reorderEnabled: true,
        selectionEnabled: false,
        popoutWholeStack: false,
        blockedPopoutsThrowError: false,
        closePopoutsOnUnload: false,
        showPopoutIcon: false,
        showMaximiseIcon: false,
        showCloseIcon: false
    },
    dimensions: {
        borderWidth: 2,
        minItemHeight: 20,
        minItemWidth: 200,
        headerHeight: 20,
        dragProxyWidth: 300,
        dragProxyHeight: 200
    },
    labels: {
        close: "close",
        maximise: "maximise",
        minimise: "minimise",
        popout: "open in new window"
    },
    content: [
        {
            type: "row",
            isClosable: true,
            title: "",
            component: "",
            content: [
                {
                    type: "column",
                    isClosable: true,
                    title: "",
                    component: "",
                    width: 15,
                    content: [
                        {
                            type: "stack",
                            isClosable: true,
                            title: "",
                            component: "",
                            height: 70,
                            content: [
                                {
                                    type: "react-component",
                                    component: dbtps.ComponentType.Property.toString(),
                                    title: language.get(language.Layout.title.property),
                                    id: dbtps.ComponentType.Property.toString(),
                                    isClosable: true
                                }
                            ]
                        },
                        {
                            type: "stack",
                            isClosable: true,
                            title: "",
                            component: "",
                            height: 30,
                            content: [
                                {
                                    type: "react-component",
                                    component: dbtps.ComponentType.Curve.toString(),
                                    title: language.get(language.Layout.title.curve),
                                    id: dbtps.ComponentType.Curve.toString(),
                                    isClosable: true
                                }
                            ]
                        }
                    ]
                },
                {
                    type: "column",
                    isClosable: true,
                    title: "",
                    component: "",
                    width: 70,
                    content: [
                        {
                            type: "stack",
                            isClosable: true,
                            title: "",
                            component: "",
                            height: 70,
                            content: [
                                {
                                    type: "react-component",
                                    component: dbtps.ComponentType.Welcome.toString(),
                                    title: language.get(language.Layout.title.welcome),
                                    id: dbtps.ComponentType.Welcome.toString(),
                                    isClosable: false
                                }
                            ]
                        },
                        {
                            type: "stack",
                            isClosable: true,
                            title: "",
                            component: "",
                            height: 30,
                            content: [
                                {
                                    type: "react-component",
                                    component: dbtps.ComponentType.Timeline.toString(),
                                    title: language.get(language.Layout.title.timeline),
                                    id: dbtps.ComponentType.Timeline.toString(),
                                    isClosable: true
                                }
                            ]
                        }
                    ]
                },
                {
                    type: "column",
                    isClosable: true,
                    title: "",
                    component: "",
                    width: 15,
                    content: [
                        {
                            type: "stack",
                            isClosable: true,
                            title: "",
                            component: "",
                            height: 70,
                            content: [
                                {
                                    type: "react-component",
                                    component: dbtps.ComponentType.Scene.toString(),
                                    title: language.get(language.Layout.title.scene),
                                    id: dbtps.ComponentType.Scene.toString(),
                                    isClosable: true
                                },
                                {
                                    type: "react-component",
                                    component: dbtps.ComponentType.ZOrder.toString(),
                                    title: language.get(language.Layout.title.zOrder),
                                    id: dbtps.ComponentType.ZOrder.toString(),
                                    isClosable: true
                                }
                            ]
                        },
                        {
                            type: "stack",
                            isClosable: true,
                            title: "",
                            component: "",
                            height: 30,
                            content: [
                                {
                                    type: "react-component",
                                    component: dbtps.ComponentType.Library.toString(),
                                    title: language.get(language.Layout.title.library),
                                    id: dbtps.ComponentType.Library.toString(),
                                    isClosable: true
                                },
                                {
                                    type: "react-component",
                                    component: dbtps.ComponentType.Animation.toString(),
                                    title: language.get(language.Layout.title.animation),
                                    id: dbtps.ComponentType.Animation.toString(),
                                    isClosable: true
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
};